<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>&Uuml;berpr&uuml;fe Login..</title>
    </head>
    <body>
        <?php
        session_start();
        require_once '../modules/UserManager/UserManager.php';
        
        // Formulardaten abfragen
        $Username = htmlspecialchars($_POST['username']);
        $Password = md5( htmlspecialchars($_POST['userpass']));
        
        $UM = new UserManager();
        if($UM->isValidUser($Username, $Password)) {
            //TODO: User anlegen?
            $User = $UM->getUser($Username);
            if(!$User) {
                $_SESSION['tabp_failed'] = true;
                $_SESSION['tabp_admin'] = 0;
                header('Location: login.php');    
            }
            else
            {
                $_SESSION['tabp_UserId']   = $User[0]->ID_User;
                $_SESSION['tabp_UserName'] = $User[0]->Name;
                $_SESSION['tabp_admin']    = $User[0]->AccessLevel;
                $_SESSION['tabp_failed']   = false;
            
                // letzter Login..
                $UM->updateLastSessionForUserId($User[0]->ID_User);
            
                header('Location: index.php?page=welcome');
            }
        } else {
            $_SESSION['tabp_failed'] = true;
            $_SESSION['tabp_admin']  = 0;
            header('Location: login.php');
        }
        ?>
    </body>
</html>
